package auto.app.controller;


import auto.app.controller.from.connectionConfigurationFrom.ConnectionConfigurationAdd;
import auto.app.controller.from.connectionConfigurationFrom.ConnectionConfigurationPageFrom;
import auto.app.controller.from.connectionConfigurationFrom.ConnectionConfigurationUpdate;
import auto.app.controller.info.BeforeAssertion.BeforeAssertionInfo;
import auto.app.controller.info.ConnecitonInfo.ConnectionConfiguraDetailInfo;
import auto.app.controller.info.ConnecitonInfo.ConnectionConfiguraPageInfo;
import auto.app.enums.ConnectionConfigurationType;
import auto.app.exetion.MainExection;
import auto.app.model.ConnectionConfiguration;
import auto.app.service.ConnectionConfigurationService;
import auto.app.util.CommonUtil;
import auto.app.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 数据库管理 前端控制器
 * </p>
 *
 * @author 杨森
 * @since 2022-10-10
 */
@RestController
@RequestMapping("/connectionConfiguration")
@Api(value = "数据库连接",tags = "数据库连接")
@AllArgsConstructor
public class ConnectionConfigurationController {
    ConnectionConfigurationService connectionConfigurationService;

    @GetMapping(value = "/type",name = "获取支持的数据库类型")
    @ApiOperation(value = "获取支持的数据库类型",notes = "获取支持的数据库类型")
    public BeforeAssertionInfo beforeAssertionMethodType(){
        List<Map<String, String>> maps = ConnectionConfigurationType.toList();
        BeforeAssertionInfo beforeAssertionInfo=new BeforeAssertionInfo();
        beforeAssertionInfo.setCode(200);
        beforeAssertionInfo.setMsg("请求成功");
        beforeAssertionInfo.setData(maps);
        return beforeAssertionInfo;
    }

    @PostMapping(name = "新增数据库连接")
    @ApiOperation(value = "新增数据库连接",notes = "新增数据库连接")
    public Result<Boolean> addConnectionConfiguration(@Valid @RequestBody ConnectionConfigurationAdd connectionConfigurationAdd ) throws MainExection {
        Boolean aBoolean = connectionConfigurationService.addConnectionConfiguration(connectionConfigurationAdd);
        return Result.getInstance(boolean.class).setData(aBoolean);
    }

    @GetMapping(value = "/{connectionId}",name = "数据库连接详情")
    @ApiOperation(value = "数据库连接详情",notes = "数据库连接详情")
    public Result<ConnectionConfiguraDetailInfo> connectionConfigurationDeatil(@PathVariable String connectionId ) throws MainExection {
        ConnectionConfiguraDetailInfo connectionConfiguraDetailInfo = connectionConfigurationService.ConnectionConfigurationDetail(connectionId);
        return Result.getInstance(ConnectionConfiguraDetailInfo.class).setData(connectionConfiguraDetailInfo);
    }

    @PutMapping(name = "数据库连接修改")
    @ApiOperation(value = "数据库连接修改",notes = "数据库连接修改")
    public Result<Boolean> connectionConfigurationUpdate(@Valid @RequestBody ConnectionConfigurationUpdate connectionConfigurationUpdate) throws MainExection {
        Boolean aBoolean = connectionConfigurationService.connectionConfigurationUpdate(connectionConfigurationUpdate);
        return  Result.getInstance(boolean.class).setData(aBoolean);
    }

    @PostMapping(value = "/list",name = "数据库连接列表")
    @ApiOperation(value = "数据库连接列表",notes = "数据库连接列表")
    public Result<ConnectionConfiguraPageInfo> connectionConfigurationList(@RequestBody ConnectionConfigurationPageFrom connectionConfigurationPageFrom)  {
        ConnectionConfiguraPageInfo connectionConfiguraPageInfo = connectionConfigurationService.connectionConfigurationList(connectionConfigurationPageFrom);
        return Result.getInstance(ConnectionConfiguraPageInfo.class).setData(connectionConfiguraPageInfo);
    }

    @DeleteMapping(value = "/{connectionId}",name = "删除数据库连接")
    @ApiOperation(value = "删除数据库连接",notes = "删除数据库连接")
    public Result<Boolean> connectionConfigurationDel(@PathVariable String connectionId) throws MainExection {
        Boolean aBoolean = connectionConfigurationService.connectionConfigurationDel(connectionId);
        return  Result.getInstance(boolean.class).setData(aBoolean);
    }

    @GetMapping(value = "/test/{connectionId}",name = "数据库连接测试")
    @ApiOperation(value = "数据库连接测试",notes = "数据库连接测试")
    public Result<Boolean> connectionConfigurationTest(@PathVariable String connectionId) throws MainExection, ClassNotFoundException {
        Boolean aBoolean = connectionConfigurationService.connectionConfigurationTest(connectionId);
        return  Result.getInstance(boolean.class).setData(aBoolean);
    }

    @GetMapping(value = "/list/all",name = "获取所有数据库连接")
    @ApiOperation(value = "获取所有数据库连接",notes = "获取所有数据库连接")
    public Result<List<ConnectionConfiguration>> connectionConfigurationAllList(){
        List<ConnectionConfiguration> list = connectionConfigurationService.list();
        return Result.getInstanceList(ConnectionConfiguration.class).setData(list);
    }

    @GetMapping(value = "/defaultUpdate/{connectionId}",name = "修改数据库为默认数据库")
    @ApiOperation(value = "修改数据库为默认数据库",notes = "修改数据库为默认数据库")
    public Result<Boolean> defaultUpdateConfiguration(@PathVariable String connectionId) throws MainExection {
        Boolean aBoolean = connectionConfigurationService.defaultUpdateConfiguration(connectionId);
        return Result.getInstance(Boolean.class).setData(aBoolean);
    }

}

